package cn.printer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * 打印机程序。
 * 面向接口编程，而不是具体的实现类
 */
@Component("printer")
public class Printer {

	//值自动注入
	@Autowired
	@Qualifier("colorInk")
	private Ink ink = null;
	@Autowired
	private Paper paper = null;

	/**
	 * 设值注入所需的setter方法。
	 * @param ink 墨盒
	 */
	public void setInk(Ink ink) {
		this.ink = ink;
	}

	/**
	 * 设值注入所需的setter方法。
	 * @param paper 纸张
	 */
	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	/**
	 * 打印方法
	 * @param message 要打印内容
	 */
	public void print(String message) {
		Random r = new Random();
		int num=r.nextInt(10);
		if (num<5){
			// 输出颜色标记
			System.out.println("使用" + ink.getColor(255, 200, 0) + "颜色打印:\n");
			// 逐字符输出到纸张
			for (int i = 0; i < message.length(); ++i) {
				paper.putInChar(message.charAt(i));
			}
			// 将纸张的内容输出
			System.out.print(paper.getContent());
		}
		if (num>=5 && num<7){
			throw new RuntimeException("卡纸了");
		}
		if (num>=7 && num<=9){
			System.out.println("没墨了");
		}
	}

	public Printer() {
	}

	public Printer(Ink ink, Paper paper) {
		this.ink = ink;
		this.paper = paper;
	}
}
